class _Promise {
  constructor (fn) {
    this.resolveQueue = []
    this.rejectQueue = []
    this.value = null
    this.status = "PENDING"
    if (typeof fn !== 'function') throw Error('_Promise必须传递一个解析函数作为参数')
    fn(this.resolve.bind(this), this.reject.bind(this))
  }
  resolve (data) {
    if (this.status !== 'PENDING') return this
    this.value = data
    this.status = 'RESOLVED'
    setTimeout(() => {
      this.resolveQueue.forEach(cb => {
        cb(this.value)
      })
    }, 0);
  }
  reject (data) {
    if (this.status !== 'PENDING') return this
    this.value = data
    this.status = 'REJECTED'
    setTimeout(() => {
      this.rejectQueue.forEach(cb => {
        cb(this.value)
      })
    }, 0);
  }
  then(resolve, reject) {
    if (this.status === 'RESOLVED') {
      setTimeout(() => {
        resolve(this.value)
      }, 0);
    }
    if (this.status === 'REJECTED') {
      setTimeout(() => {
        reject(this.value)
      }, 0);
    }
    // if (this.status)
    // 待resolved
    if (this.status === 'PENDING') {
      this.resolveQueue.push(resolve)
      this.rejectQueue.push(reject)
    }
    return this
  }
}
_Promise.resolve = data => {
  let pro = new _Promise(() => {})
  pro.resolve(data)
  return pro
}

_Promise.reject = data => {
  let pro = new _Promise(() => {})
  pro.reject(data)
  return pro
}
new _Promise((res, rej) => {
  setTimeout(() => {
    res(`
      夕阳漫不经心走下酒坊
      晚风路过陶坛吹散了酒香
      偷倒空的酒坛还挂在墙上
      猫咪抱着尾巴睡得好安详
      我记得你所有的模样
    `)
  }, 1000);
}).then((res) => {
  console.log(res)
}, (res) => {
  console.log(res)
})
console.log('123')
